﻿using System;
using System.Web;
using System.Web.Routing;
using ManagedUIFlow.Model;
using Moq;
using Xunit;

namespace ManagedUIFlow.Specs.Model
{
    public class AppControllerExecuteProcess
    {    
        private readonly RequestContext _requestContext;

        public AppControllerExecuteProcess()
        {
            _requestContext = new RequestContext(new Mock<HttpContextBase>().Object, new RouteData());
        }

        [Fact]
        public void ExecuteWithNullFlowAndContext_Throws()
        {
            Assert.Throws<ArgumentNullException>(() => ApplicationController.Current.Execute(null, null));
        }       

        [Fact]
        public void Execute_TryExecutingCommand()
        {
            var flow = new ManagedFlow(Guid.NewGuid(), "foo", "fooUp", RedirectBehavior.Manual, string.Empty);
            var commandMock = new Mock<ICommand>();
            commandMock.Setup(c => c.Execute(flow)).Verifiable();
            var commandBuilder = new CommandBuilder();
            var commandFactoryMock = new Mock<ICommandFactory>();
            commandBuilder.SetCommandFactory(commandFactoryMock.Object);
            commandFactoryMock.Setup(c => c.CreateCommand(_requestContext, flow)).Returns(commandMock.Object);
            commandFactoryMock.Setup(c => c.ReleaseCommand(commandMock.Object));
            var applicationController = new ApplicationController
                                            {
                                                CommandBuilder = commandBuilder
                                            };

            applicationController.Execute(_requestContext, flow);

            commandMock.Verify();           
        }
    }
}